package com.skywolf.chem.usercenter.controller;


import com.skywolf.chem.common.sysconfig.response.RespData;
import com.skywolf.chem.common.sysconfig.utils.TLBeanUtils;
import com.skywolf.chem.usercenter.entity.Role;
import com.skywolf.chem.usercenter.pojo.param.UserRoleParam;
import com.skywolf.chem.usercenter.pojo.vo.RoleVO;
import com.skywolf.chem.usercenter.service.IUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 系统用户和角色关系表 前端控制器
 * </p>
 *
 * @author
 * @since 2019-12-03
 */
@Api(value = "管理后台用户角色API", description = "管理后台用户角色API")
@RestController
@RequestMapping("/usercenter/user-role")
public class UserRoleController {

    @Autowired
    private IUserRoleService userRoleService;

    /**
     * 保存用户角色
     *
     * @param userRoleParam
     * @return
     */
    @ApiOperation(value = "保存用户角色")
    @PostMapping()
    public RespData saveUserRole(@RequestBody @Validated UserRoleParam userRoleParam) {
        userRoleService.saveUserRole(userRoleParam);
        return RespData.success();
    }

    /**
     * 查询用户角色
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "获取用户角色")
    @GetMapping("/{userId}")
    public RespData listUserRole(@PathVariable Long userId) {
        List<Role> roleList = userRoleService.listUserRole(userId);
        List<RoleVO> roleVOList = Collections.emptyList();
        if (!CollectionUtils.isEmpty(roleList)) {
            roleVOList = TLBeanUtils.copyList(roleList, RoleVO.class);
        }
        return RespData.success(roleVOList);
    }
}
